<?php
require_once(dirname(__FILE__).'/Abstract.php');
class Consultation extends AbstractPatientController 
{
    public function indexAction() 
    {
        $d = array();
        $d['title'] = $this->l('Select an start a consultation');
        //$this->load->model('ORM/User/UserEntityORM');
        $d['consultations'] = $this->ConsultationMetier->getConsultations($config = array('id_patient'=>$this->session->userdata('id_user')));
        $d['doctors'] = $this->ConsultationMetier->getAllDoctors();//($this->getConfig('idRolePatient'));
        $d['consultations_types'] = $this->ConsultationMetier->getConsultationsTypes();
        $d['connected_doctors'] = $this->UserEntityORM->getAllByIdRoleAndIsConnected(self::ID_DOCTOR_ROLE, '1');
        
        $d['title_for_layout'] = $this->l('Consultations list.');
        $d['menu'] = 'consultations';
        $d['page_name'] = 'consultations_list';
        
        $this->load->view('Templates/Layouts/Patient/header', $d);
        $this->load->view('Patient/Consultation/index', $d);
        $this->load->view('Templates/Layouts/Patient/footer', $d);
    }
    
    public function startAction()
    {
        $d = array();
        $d['title'] = $this->l('Start a consultation');
        // $this->load->model('Metier/Consultation/ConsultationMetier');
        if ($this->input->post('btn_submit') !== false){
             // Initialized the form validation
            $config = array(
               array(
                     'field'   => 'id_doctor', 
                     'label'   => $this->l('Type'), 
                     'rules'   => 'trim|required|xss_clean'
                  ),
               array(
                     'field'   => 'id_type', 
                     'label'   => $this->l('Doctor'), 
                     'rules'   => 'trim|required|xss_clean'
                  ),
            );
            $this->form_validation->set_rules($config);
            if ($this->form_validation->run() == FALSE ){
                $this->session->set_flashdata('errorMessage', $this->l('An error occured. Please check filled data and try again.'));
            } else {
                $this->load->model('ORM/Consultation/ConsultationORM');
                // Initialize the new record
                $record = array(
                    'id_doctor'      => $this->input->post('id_doctor'),
                    'id_type'        => $this->input->post('id_type'),
                    'id_patient'     => $this->session->userdata('id_user'),                   
                    // Automatic fill                           
                    'token_session'  => $this->session->userdata('session_id'),
                    'date_add'       => @date('Y-m-d H:i:s'),
                    'date_upd'       => @date('Y-m-d H:i:s'),
                );
                // Create the new message
                if ($this->ConsultationORM->save($record)) {
                    $last_consultation_id = $this->db->insert_id();
                    //Get the amount of this consultation
                    $consultation_type = $this->ConsultationMetier->getConsultationsTypes($config = array('id_type' => $this->input->post('id_type')));

                    $record_history = array(
                        // Automatic fill 
                        'id_consultation'  => $last_consultation_id,
                        'id_status'        => '1',   
                        'date'             => @date('Y-m-d H:i:s'),
                    );
                    $record_payment = array(
                        // Automatic fill 
                        'id_consultation'  => $last_consultation_id,
                        'amount'           => $consultation_type['tarif'],
                        'detail'           => $consultation_type['label'],   
                        'date_add'         => @date('Y-m-d H:i:s'),
                    );
                    $this->load->model('ORM/Consultation/ConsultationHistoryORM');
                    $this->ConsultationHistoryORM->save($record_history);
                    $this->load->model('ORM/Consultation/ConsultationPaymentORM');
                    $this->ConsultationPaymentORM->save($record_payment);
                    // Redirect to the consultation create form
                    redirect('Patient/Consultation/Payment');
                    die();
                } else {
                    $this->session->set_flashdata('errorMessage', $this->l('An error occured while saving your message. Please try again.'));
                }
            }
        } else {
            // Redirect to the consultation create form
            redirect('Patient/Consultation/');
        }     
    }

    public function paymentAction()
    {
        $d = array();
        $d['title'] = $this->l('Pay for a consultation');

        $d['title_for_layout'] = $this->l('Payment for this consultation.');
        $d['menu'] = 'consultations';
        $d['page_name'] = 'consultation_payment';
        
        $this->load->view('Templates/Layouts/Patient/header', $d);
        $this->load->view('Patient/Consultation/index', $d);
        $this->load->view('Templates/Layouts/Patient/footer', $d);
    }
}

